home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / libshade / viewing.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  9.8 KB  |  338 lines

  1. /*
  2.  * viewing.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb, Rod G. Bogart
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * viewing.c,v 4.1 1994/08/09 08:05:11 explorer Exp
  17.  *
  18.  * viewing.c,v
  19.  * Revision 4.1  1994/08/09  08:05:11  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:19  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0.1.1  91/09/29  15:53:09  cek
  26.  * patch1: Added support for window and crop commands.
  27.  * 
  28.  * Revision 4.0  91/07/17  14:48:18  kolb
  29.  * Initial version.
  30.  * 
  31.  */
  32. #include "rayshade.h"
  33. #include "viewing.h"
  34. #include "libcommon/sampling.h"
  35. #include "options.h"
  36. #include "defaults.h"
  37. #include "picture.h"
  38. #include "stats.h"
  39. #include "zbuf.h"
  40.  
  41. RSCamera    Camera;
  42. RSScreen    Screen;
  43.  
  44. void SampleScreen(), SampleScreenFiltered();
  45.  
  46. void
  47. RSViewing()
  48. {
  49.     Float magnitude;
  50.  
  51.     VecSub(Camera.lookp, Camera.pos, &Camera.dir);
  52.     Screen.firstray = Camera.dir;
  53.  
  54.     Camera.lookdist = VecNormalize(&Camera.dir);
  55.     if (VecNormCross(&Camera.dir, &Camera.up, &Screen.scrni) == 0.)
  56.         RLerror(RL_PANIC,
  57.             "The view and up directions are identical?\n");
  58.     (void)VecNormCross(&Screen.scrni, &Camera.dir, &Screen.scrnj);
  59.  
  60.     /*
  61.      * Add stereo separation if desired.
  62.      */
  63.     if (Options.stereo) {
  64.         if (Options.stereo == LEFT)
  65.             magnitude = -.5 * Options.eyesep;
  66.         else
  67.             magnitude =  .5 * Options.eyesep;
  68.         Camera.pos.x += magnitude * Screen.scrni.x;
  69.         Camera.pos.y += magnitude * Screen.scrni.y;
  70.         Camera.pos.z += magnitude * Screen.scrni.z;
  71.         VecSub(Camera.lookp, Camera.pos, &Screen.firstray);
  72.         Camera.dir = Screen.firstray;
  73.         Camera.lookdist = VecNormalize(&Camera.dir);
  74.         (void)VecNormCross(&Camera.dir, &Camera.up, &Screen.scrni);
  75.         (void)VecNormCross(&Screen.scrni, &Camera.dir, &Screen.scrnj);
  76.     }
  77.  
  78.     magnitude = 2.*Camera.lookdist * tan(deg2rad(0.5*Camera.hfov)) /
  79.                 Screen.xres;
  80.  
  81.     VecScale(magnitude, Screen.scrni, &Screen.scrnx);
  82.     magnitude = 2.*Camera.lookdist * tan(deg2rad(0.5*Camera.vfov)) /
  83.                 Screen.yres;
  84. #ifndef URT
  85.     /*
  86.      * If using "generic" file format, render top-to-bottom (yick).
  87.      */
  88.     magnitude *= -1;
  89. #endif
  90.     VecScale(magnitude, Screen.scrnj, &Screen.scrny);
  91.  
  92.     Screen.firstray.x -= 0.5*Screen.yres*Screen.scrny.x +
  93.                  0.5*Screen.xres*Screen.scrnx.x;
  94.     Screen.firstray.y -= 0.5*Screen.yres*Screen.scrny.y +
  95.                  0.5*Screen.xres*Screen.scrnx.y;
  96.     Screen.firstray.z -= 0.5*Screen.yres*Screen.scrny.z +
  97.                  0.5*Screen.xres*Screen.scrnx.z;
  98.  
  99.     /* Firstray now points to the lower left corner of the image plane,
  100.        if fixed at the eye position */
  101.  
  102.     if (Camera.focaldist == UNSET)
  103.         Camera.focaldist = Camera.lookdist;
  104. }
  105.  
  106. /*
  107.  * Adjust the initial ray to account for an aperture and a focal
  108.  * distance.  The ray argument is assumed to be an initial ray, and
  109.  * always reset to the eye point.  It is assumed to be unit length.
  110.  */
  111. void
  112. focus_blur_ray(ray)
  113. Ray *ray;
  114. {
  115.     Vector circle_point, aperture_inc;
  116.     extern void UnitCirclePoint();
  117.     /*
  118.      * Find a point on a unit circle and scale by aperture size.
  119.      * This simulates rays passing thru different parts of the aperture.
  120.      * Treat the point as a vector and rotate it so the circle lies
  121.      * in the plane of the screen.  Add the aperture increment to the
  122.      * starting position of the ray.  Stretch the ray to be focaldist 
  123.      * in length.  Subtract the aperture increment from the end of the
  124.      * long ray.  This insures that the ray heads toward a point at
  125.      * the specified focus distance, so that point will be in focus.
  126.      * Normalize the ray, and that's it.  Really.
  127.      */
  128.     UnitCirclePoint(&circle_point, ray->sample);
  129.     VecComb(Camera.aperture * circle_point.x, Screen.scrni,
  130.             Camera.aperture * circle_point.y, Screen.scrnj,
  131.             &aperture_inc);
  132.     VecAdd(aperture_inc, Camera.pos, &(ray->pos));
  133.     VecScale(Camera.focaldist, ray->dir, &(ray->dir));
  134.     VecSub(ray->dir, aperture_inc, &(ray->dir));
  135.     (void)VecNormalize(&ray->dir);
  136. }
  137.  
  138. void
  139. ViewingSetup()
  140. {
  141. #define SWAP(a,b)    (tmp = (a), (a) = (b), (b) = tmp)
  142.  
  143.     Float tmp;
  144.     int xwidth, ywidth;
  145.  
  146.     if (Options.stereo && Options.eyesep == UNSET)
  147.         RLerror(RL_PANIC,
  148.             "No eye separation specified for stereo rendering.\n");
  149.     /*
  150.      * Because we want the user to be able to override the input file
  151.      * through the command line, we have to initialize some variables to
  152.      * bogus values so that when the file is being parsed, it is
  153.      * possible to tell if a given variable has been set on the
  154.      * command line.
  155.      *
  156.      * If such variables are not set to legal values on the command
  157.      * line or in the input file, we must do it here.
  158.      */
  159.     if (Screen.xres == UNSET)
  160.         Screen.xres = XRESOLUTION;
  161.     if (Screen.yres == UNSET)
  162.         Screen.yres = YRESOLUTION;
  163.  
  164.     /*
  165.      * The window to be rendered is defined by applying
  166.      * the crop window to the sub window.  The subwindow
  167.      * is defined using pixel numbers, and must be within
  168.      * [0, xres -1],[0, yres -1].  The default is the entire
  169.      * screen.  The crop window is specified using normalized
  170.      * coordinates.
  171.      */
  172.  
  173.     if (!Options.window_set) {
  174.         /* If no window set, set equal to entire screen. */    
  175.         Options.window[LOW][X] = Options.window[LOW][Y] = 0;
  176.         Options.window[HIGH][X] = Screen.xres -1;
  177.         Options.window[HIGH][Y] = Screen.yres -1;
  178.     }
  179.  
  180.     /* Truncate crop window to legal limits. */
  181.     if (Options.crop[LOW][X] > Options.crop[HIGH][X])
  182.         SWAP(Options.crop[LOW][X], Options.crop[HIGH][X]);
  183.     if (Options.crop[LOW][Y] > Options.crop[HIGH][Y])
  184.         SWAP(Options.crop[LOW][Y], Options.crop[HIGH][Y]);
  185.     if (Options.crop[LOW][X] < 0.) Options.crop[LOW][X] = 0.;
  186.     if (Options.crop[LOW][Y] < 0.) Options.crop[LOW][Y] = 0.;
  187.     if (Options.crop[HIGH][X] > 1.) Options.crop[HIGH][X] = 1.;
  188.     if (Options.crop[HIGH][Y] > 1.) Options.crop[HIGH][Y] = 1.;
  189.  
  190.     xwidth = Options.window[HIGH][X] - Options.window[LOW][X];
  191.     ywidth = Options.window[HIGH][Y] - Options.window[LOW][Y];
  192.  
  193.     /* Compute x and y extents of window to be renered. */
  194.     Screen.minx = (int)(Options.window[LOW][X] +
  195.             Options.crop[LOW][X] * xwidth);
  196.     Screen.maxx = (int)(Options.window[LOW][X] +
  197.             Options.crop[HIGH][X] * xwidth);
  198.     Screen.miny = (int)(Options.window[LOW][Y] +
  199.             Options.crop[LOW][Y] * ywidth);
  200.     Screen.maxy = (int)(Options.window[LOW][Y] +
  201.             Options.crop[HIGH][Y] * ywidth);
  202.  
  203. #ifdef URT
  204.     /*
  205.      * If using the URT, we should use the RLE file header to
  206.      * determine cropped window size.  Screen size
  207.      * (Screen.xres, Screen.yres) is determined from command
  208.      * line or input file, as usual.
  209.      *
  210.      * If the cropped window computed in PictureSetWindow()
  211.      * is not equal the cropped window computed above,
  212.      * a warning message is issued.
  213.      */
  214.     if (Options.appending) {
  215.         /*
  216.          * Read image header to determine window size.
  217.          */
  218.         PictureSetWindow();
  219.     }
  220. #endif
  221.  
  222.     Screen.xsize = Screen.maxx - Screen.minx + 1;
  223.     Screen.ysize = Screen.maxy - Screen.miny + 1;
  224.  
  225.     /*
  226.      * Sanity check.
  227.      */
  228.     if (Screen.minx < 0 || Screen.miny < 0 ||
  229.         Screen.maxx >= Screen.xres || Screen.maxy >= Screen.yres)
  230.         RLerror(RL_PANIC, "Invalid window specification.\n");
  231.  
  232.     /*
  233.      * If not defined in the input file, calculate VFOV
  234.      * by hand.  This assumes that pixels are square, which is
  235.      * probably a bad idea.  ("aspect" option?)
  236.      */
  237.     if (Camera.vfov == UNSET)
  238.         Camera.vfov = Camera.hfov * Screen.yres / Screen.xres;
  239. }
  240.  
  241. void
  242. SampleScreenFiltered(x, y, u, v, ray, color, sample)
  243. Float x, y;
  244. Ray *ray;
  245. Pixel *color;
  246. int sample, u, v;
  247. {
  248.     SampleScreen(x, y, ray, color, sample);
  249.     color->r *= Sampling.filter[u][v];
  250.     color->g *= Sampling.filter[u][v];
  251.     color->b *= Sampling.filter[u][v];
  252.     color->alpha *= Sampling.filter[u][v];
  253. }    
  254.  
  255. void
  256. SampleScreen(x, y, ray, color, sample)
  257. Float x, y;        /* Screen position to sample */
  258. Ray *ray;        /* ray, with origin and medium properly set */
  259. Pixel *color;        /* resulting color */
  260. int sample;        /* sample number */
  261. {
  262.     Float dist, temp;
  263.     HitList hitlist;
  264.     Color ctmp, fullintens;
  265.     extern void focus_blur_ray(), ShadeRay();
  266.  
  267.     /*
  268.      * Calculate ray direction.
  269.      */
  270.     Stats.EyeRays++;
  271.     ray->type = EYE_RAY;
  272.     ray->dir.x = Screen.firstray.x + x*Screen.scrnx.x + y*Screen.scrny.x;
  273.     ray->dir.y = Screen.firstray.y + x*Screen.scrnx.y + y*Screen.scrny.y;
  274.     ray->dir.z = Screen.firstray.z + x*Screen.scrnx.z + y*Screen.scrny.z;
  275.  
  276.     (void)VecNormalize(&ray->dir);
  277.  
  278.     ray->sample = sample;
  279.  
  280.     if (Camera.aperture > 0.0) {
  281.         /*
  282.          * If the aperture is open, adjust the initial ray
  283.          * to account for depth of field.  
  284.          */
  285.         focus_blur_ray(ray);
  286.     }
  287.  
  288.     /*
  289.      * Do the actual ray trace.
  290.      */
  291.     fullintens.r = fullintens.g = fullintens.b = 1.;
  292.     dist = FAR_AWAY;
  293.     hitlist.nodes = 0;
  294.         ray->viewpoint = ray->pos;
  295.         ray->viewdist = 0.;
  296.         ray->width = 2.*tan(deg2rad(Camera.hfov/2.))/Screen.xres;
  297.         if ((temp = 2.*tan(deg2rad(Camera.vfov/2.))/Screen.yres) > ray->width) ray->width = temp;
  298.         ray->stretch = 1.;
  299.  
  300.     (void)TraceRay(ray, &hitlist, EPSILON, &dist);
  301.  
  302.     ShadeRay(&hitlist, ray, dist, &Screen.background, &ctmp, &fullintens);
  303.     color->r = ctmp.r;
  304.     color->g = ctmp.g;
  305.     color->b = ctmp.b;
  306.     if (hitlist.nodes != 0) 
  307.     {
  308.         Float d = hitlist.data[hitlist.nodes-1].dist;
  309.  
  310.         d *= dotp(&Camera.dir, &hitlist.data[hitlist.nodes-1].ray.dir);
  311.         ZbufAdd (x, y, d); /* Save depth in Z buffer */
  312.         color->alpha = 1.;
  313.     }
  314.     else 
  315.     {
  316.         color->alpha = 0.;
  317.     }
  318. }
  319.  
  320. Float PixelSize(ray, dist)
  321. Ray *ray;
  322. Float dist;
  323. {
  324.         Float s, d;
  325.  
  326.         d = dist / ray->stretch;
  327.         if (ray->viewdist < EPSILON)  /* eye rays */ 
  328.                 s = ray->width * d;
  329.         else if (ray->viewdist < FAR_AWAY)   /* infinite light sources !! */
  330.                 s = ray->width / ray->viewdist * (d + ray->viewdist);
  331.       else 
  332.               s = ray->width;
  333.         s = fabs(s);
  334.         if (s < EPSILON) return EPSILON;
  335.  
  336.         return s;
  337. }
  338.